﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Commander_Wars.Spieletools
Public MustInherit Class ObjektPanel
    Inherits Objekt2D
    Protected PanelObjekte As New Stack(Of Objekt2D)
    Public InternePosition As Point
    Protected Anchor As Integer = 0
    Protected CheckEvents As Boolean
    Public Visible As Boolean = True
    Protected ScrollBars As Boolean = False
    ''' <summary>
    ''' So groß is das Rechteck tatsächlich!
    ''' X=0=Koordinate.X
    ''' Y=0=Koordinate.Y
    ''' </summary>
    ''' <remarks></remarks>
    Protected RealDimension As Rectangle
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Größe"></param>
    ''' <param name="Fixed"></param>
    ''' <param name="Anchor">0=Keiner,1=Alles,2=Rechts Links Fixed,3=Alle Ecken bewegen sich,4=Links,Rechts,Unten</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal Größe As Rectangle, ByVal Fixed As PointF, ByVal Anchor As Integer, ByVal CheckEvents As Boolean)
        MyBase.New("ValueRegler", Größe, Größe.Location, Fixed)
        Me.Anchor = Anchor
        Me.CheckEvents = CheckEvents
        RealDimension = Größe


    End Sub
    Public Overrides Sub Malen()
        If ScrollBars Then
            YIndexMax = Bildgröße.Height - 22 - 15
            XIndexMax = Bildgröße.Width - 22 - 15
        End If
        DrawScrollBar()
    End Sub
    Public MustOverride Sub MouseMove(ByVal X As Integer, ByVal Y As Integer)
    Public Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        MouseMove(e.X, e.Y)
    End Sub

#Region "Scroll Bar"
    ''' <summary>
    ''' wenn true haben wir auf diesen Regler Button geklickt und wechseln hoch runter
    ''' </summary>
    ''' <remarks></remarks>
    Protected AktivY As Boolean = False

    ''' <summary>
    ''' wenn true haben wir auf diesen Regler Button geklickt und wechseln hoch runter
    ''' </summary>
    ''' <remarks></remarks>
    Protected AktivX As Boolean = False
    Protected IndexY As Integer = 0
    Protected YIndexMax As Integer = 0
    Protected IndexX As Integer = 0
    Protected XIndexMax As Integer = 0
    Private Sub DrawScrollBar()
        If ScrollBars Then
            Dim sngRotation As Double = 0
            Dim intColor As Color = Color.FromArgb(255, 255, 255, 255)
            'Zuerst den Balken runter!
            Dim ValueBreite As Integer = Bildgröße.Height - 22
            Grafikengine.ptfRotCenter = New Point(0, 0)

            With Grafikengine.recBild
                .X = 0 : .Width = 16
                .Y = 0 : .Height = 11
            End With

            Grafikengine.ptfPosition = Koordinaten
            Grafikengine.szfScale = Nothing
            'wir malen den oberen Pfeil
            Grafikengine.ptfPosition.X += Rechteck.Width
            Grafikengine.Sprite.Draw2D(TexturenHalter.GetDropDownItemTexturen(2), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)

            'wir malen den Unteren Pfeil
            With Grafikengine.recBild
                .X = 0 : .Width = 16
                .Y = 41 : .Height = 11
            End With
            Grafikengine.ptfPosition = New Point(Koordinaten.X + Rechteck.Width, Koordinaten.Y + ValueBreite + 11)
            Grafikengine.Sprite.Draw2D(TexturenHalter.GetDropDownItemTexturen(2), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)

            'Wir finden heraus wie oft wir den Mittelteilmalen müssen
            Dim Breite As Integer = Divi(ValueBreite, 15)
            'Wir malen alle Mittelteile bis auf den letzten
            With Grafikengine.recBild
                .X = 0 : .Width = 16
                .Y = 26 : .Height = 15
            End With
            Grafikengine.ptfPosition.Y = Koordinaten.Y + 11
            For I = 0 To Breite - 2
                Grafikengine.Sprite.Draw2D(TexturenHalter.GetDropDownItemTexturen(2), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
                Grafikengine.ptfPosition.Y += 15
            Next
            'Der letzte wird jetzt gemalt, weil er eine Ausnahme ist.
            Grafikengine.ptfPosition.Y = Koordinaten.Y + 11 + ValueBreite - 15
            Grafikengine.Sprite.Draw2D(TexturenHalter.GetDropDownItemTexturen(2), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            'So jetzt müssen wir noch den Regler malen
            Grafikengine.ptfPosition.Y = IndexY + Koordinaten.Y + 11

            With Grafikengine.recBild
                .X = 0 : .Width = 16
                .Y = 11 : .Height = 15
            End With

            Grafikengine.Sprite.Draw2D(TexturenHalter.GetDropDownItemTexturen(2), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            'Jetzt den Balken Unten!
            ValueBreite = Bildgröße.Width - 22
            With Grafikengine.recBild
                .X = 0 : .Width = 11
                .Y = 0 : .Height = 16
            End With

            Grafikengine.ptfPosition = Koordinaten
            Grafikengine.szfScale = Nothing
            Dim T As Texture = Objekt2D.Texturen(TexturID).Textur
            'wir malen den oberen Pfeil
            Grafikengine.ptfPosition.Y += Rechteck.Height
            Grafikengine.Sprite.Draw2D(T, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            'wir malen den Unteren Pfeil
            With Grafikengine.recBild
                .X = 41 : .Width = 11
                .Y = 0 : .Height = 16
            End With
            Grafikengine.ptfPosition = New Point(Koordinaten.X + ValueBreite + 11, Koordinaten.Y + Rechteck.Height)
            Grafikengine.Sprite.Draw2D(T, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)

            'Wir malen alle Mittelteile bis auf den letzten
            Breite = Divi(ValueBreite, 15)
            With Grafikengine.recBild
                .X = 26 : .Width = 15
                .Y = 0 : .Height = 16
            End With
            Grafikengine.ptfPosition.X = Koordinaten.X + 11
            For I = 0 To Breite - 2
                Grafikengine.Sprite.Draw2D(T, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
                Grafikengine.ptfPosition.X += 15
            Next
            'Der letzte wird jetzt gemalt, weil er eine Ausnahme ist.
            Grafikengine.ptfPosition.X = Koordinaten.X + 11 + ValueBreite - 15
            Grafikengine.Sprite.Draw2D(T, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            'So jetzt müssen wir noch den Regler malen
            Grafikengine.ptfPosition.X = IndexX + Koordinaten.X + 11
            With Grafikengine.recBild
                .X = 11 : .Width = 15
                .Y = 0 : .Height = 16
            End With

            Grafikengine.Sprite.Draw2D(T, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        End If
    End Sub
    Protected Sub CheckScrollBar(ByVal e As System.Windows.Forms.MouseEventArgs)
        CheckScrollBar(e.X, e.Y)
    End Sub
    Protected Sub CheckScrollBar(ByVal X As Integer, ByVal Y As Integer)
        Dim R As New Rectangle(Koordinaten.X + Rechteck.Width, Koordinaten.Y, 16, Bildgröße.Height)
        If R.Contains(X, Y) Then
            R.Height = 11
            If R.Contains(X, Y) Then
                IndexY -= 1
                If IndexY < 0 Then IndexY = 0
            Else
                R.Y = Koordinaten.Y + Bildgröße.Height - 11
                If R.Contains(X, Y) Then
                    IndexY += 1
                    If IndexY > YIndexMax Then IndexY = YIndexMax
                Else
                    AktivY = True
                End If
            End If
        Else
            R = New Rectangle(Koordinaten.X, Koordinaten.Y + Rechteck.Height, Bildgröße.Width, 16)
            If R.Contains(X, Y) Then
                R.Width = 11
                If R.Contains(X, Y) Then
                    IndexX -= 1
                    If IndexX < 0 Then IndexX = 0
                Else
                    R.X = Koordinaten.X + Bildgröße.Width - 11
                    If R.Contains(X, Y) Then
                        IndexX += 1
                        If IndexX > XIndexMax Then IndexX = XIndexMax
                    Else
                        AktivX = True
                    End If
                End If
            End If
        End If
        CalculateInternePosition()
    End Sub
    Protected Sub MouseMoveScrollBar(ByVal X As Integer, ByVal Y As Integer)
        If AktivY Then
            MouseMoveScrollBarY(Y)
        ElseIf AktivX Then
            MouseMoveScrollBarX(X)
        End If
        CalculateInternePosition()
    End Sub
    Protected Sub MouseMoveScrollBarY(ByVal Y As Integer)
        If Koordinaten.Y + 11 > Y Then
            IndexY = 0
        ElseIf Koordinaten.Y + Bildgröße.Height - 11 - 15 < Y Then
            IndexY = YIndexMax
        Else
            IndexY = Y - Koordinaten.Y - 11
        End If
    End Sub
    Protected Sub MouseMoveScrollBarX(ByVal X As Integer)
        If Koordinaten.X + 11 > X Then
            IndexX = 0
        ElseIf Koordinaten.X + Bildgröße.Width - 11 - 15 < X Then
            IndexX = XIndexMax
        Else
            IndexX = X - Koordinaten.X - 11
        End If
    End Sub
    Protected Sub CalculateInternePosition()
        If ScrollBars Then
            If YIndexMax <> 0 Then
                InternePosition.Y = -((IndexY / YIndexMax) * (RealDimension.Height - RealDimension.Y - Rechteck.Height))
            End If
            If XIndexMax <> 0 Then
                InternePosition.X = -((IndexX / XIndexMax) * (RealDimension.Width - RealDimension.X - Rechteck.Width))
            End If
        End If
    End Sub
    Protected Sub MouseUpScrollBar()
        AktivX = False
        AktivY = False
    End Sub
    Protected Sub KeyDownScrollBar(ByVal e As System.Windows.Forms.KeyEventArgs)
        If ScrollBars Then
            'Tasten Eingabe!
            Select Case e.KeyCode
                Case My.Settings.Oben
                    IndexY -= 1
                    If IndexY < 0 Then IndexY = 0
                    CalculateInternePosition()
                Case My.Settings.Unten
                    IndexY += 1
                    If IndexY > YIndexMax Then IndexY = YIndexMax
                    CalculateInternePosition()
                Case My.Settings.Rechts
                    IndexX += 1
                    If IndexX > XIndexMax Then IndexX = XIndexMax
                    CalculateInternePosition()
                Case My.Settings.Links
                    IndexX -= 1
                    If IndexX < 0 Then IndexX = 0
                    CalculateInternePosition()
            End Select
        End If
    End Sub
#End Region

    Public Overrides Sub ChangeWindowSize(ByVal PosChange As System.Drawing.Point)
        Select Case Anchor
            Case 0 'Keiner
                Bildgröße.X += (PosChange.X * Fixed.X)
                Bildgröße.Y += (PosChange.Y * Fixed.Y)
            Case 1 'Alles
                Bildgröße.Width += PosChange.X
                Bildgröße.Height += PosChange.Y
            Case 2 'Wir machen nichts
            Case 3 'Beide Bewegen
                Bildgröße.X += (PosChange.X * Fixed.X)
                Bildgröße.Y += (PosChange.Y * Fixed.Y)
                Bildgröße.Width += (PosChange.X * Fixed.X)
                Bildgröße.Height += (PosChange.Y * Fixed.Y)
            Case 4
                Bildgröße.X += (PosChange.X * Fixed.X)
                Bildgröße.Width += (PosChange.X * Fixed.X)
            Case Else
                Bildgröße.X += (PosChange.X * Fixed.X)
                Bildgröße.Y += (PosChange.Y * Fixed.Y)
        End Select
        CheckScrollBarActivation()
    End Sub
    Protected Overridable Sub CheckScrollBarActivation()
        Dim Activate As Boolean = False
        For Each Objekt In PanelObjekte
            If Not Activate Then
                Activate = CheckActivation(Objekt)
            Else
                Exit For
            End If

        Next
        If Not Activate Then
            RemoveScrollBars()
        End If
    End Sub
    Protected Sub RemoveScrollBars()
        If ScrollBars Then
            ScrollBars = False
            Bildgröße.Width += 16
            Bildgröße.Height += 16
            RealDimension.Width -= 60
            RealDimension.Height -= 60
        End If
    End Sub
    Public Sub AddObjekt(ByVal O As Objekt2D)
        PanelObjekte.Push(O)
        CheckActivation(O)
    End Sub
    Private Function CheckActivation(ByVal O As Objekt2D) As Boolean
        If TypeOf O Is ObjektKOCheckBox Then
            If Not InVisibleArea(New Rectangle(O.Koordinaten.X - 15, O.Koordinaten.Y - 53, O.Rechteck.Width + 30, O.Rechteck.Height + 53), True) Then
                ActivateScrollBars()
                If ScrollBars Then
                    If O.Koordinaten.X - 15 - 10 < RealDimension.X Then
                        RealDimension.X = O.Koordinaten.X - 15 - 10
                    End If
                    If O.Koordinaten.Y - 53 - 10 < RealDimension.Y Then
                        RealDimension.Y = O.Koordinaten.Y - 53 - 10
                    End If
                    If O.Rechteck.Width + 30 + O.Koordinaten.X + 10 > RealDimension.Width Then
                        RealDimension.Width = O.Rechteck.Width + 30 + O.Koordinaten.X + 10
                    End If
                    If O.Rechteck.Height + O.Koordinaten.Y + 53 + 10 > RealDimension.Height Then
                        RealDimension.Height = O.Rechteck.Height + 53 + O.Koordinaten.Y + 10
                    End If
                    Return True
                End If
            End If
        Else
            If Not InVisibleArea(New Rectangle(O.Koordinaten.X, O.Koordinaten.Y, O.Rechteck.Width + 17, O.Rechteck.Height), True) Then
                ActivateScrollBars()
            End If
            If ScrollBars Then
                If O.Koordinaten.X - 10 < RealDimension.X Then
                    RealDimension.X = O.Koordinaten.X - 10
                End If
                If O.Koordinaten.Y - 10 < RealDimension.Y Then
                    RealDimension.Y = O.Koordinaten.Y - 10
                End If
                If O.Rechteck.Width + 17 + O.Koordinaten.X + 10 > RealDimension.Width Then
                    RealDimension.Width = O.Rechteck.Width + 17 + O.Koordinaten.X + 10
                End If
                If O.Rechteck.Height + O.Koordinaten.Y + 10 > RealDimension.Height Then
                    RealDimension.Height = O.Rechteck.Height + O.Koordinaten.Y + 10
                End If
                Return True
            End If
        End If
        Return False
    End Function
    ''' <summary>
    ''' Aktiviert die ScrollBars
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub ActivateScrollBars()
        If Not ScrollBars Then
            ScrollBars = True
            Bildgröße.Width -= 16
            Bildgröße.Height -= 16
            RealDimension.Width += 60
            RealDimension.Height += 60
        End If
    End Sub
    Public Function IsVisible(ByVal R As Rectangle, ByVal MitIntern As Boolean) As Boolean
        If Visible Then
            Return InVisibleArea(R, MitIntern)
        Else
            Return False
        End If
    End Function
    Public Function InVisibleArea(ByVal R As Rectangle, ByVal MitIntern As Boolean)
        If MitIntern Then
            If Bildgröße.Contains(New Rectangle(R.X + InternePosition.X + Koordinaten.X, R.Y + InternePosition.Y + Koordinaten.Y, R.Width, R.Height)) Then
                Return True
            Else
                Return False
            End If
        Else
            If Bildgröße.Contains(New Rectangle(R.X, R.Y, R.Width, R.Height)) Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
    Public Overrides Sub RemoveHandles()
        While PanelObjekte.Count > 0
            PanelObjekte.Pop()
        End While
        MyBase.RemoveHandles()
    End Sub
End Class
Public Class ObjektPanel2D
    Inherits ObjektPanel
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Größe"></param>
    ''' <param name="Fixed"></param>
    ''' <param name="Anchor">0=Keiner,1=Alles,2=Rechts Links Fixed,3=Alle Ecken bewegen sich,4=Links,Rechts,Unten</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal Größe As Rectangle, ByVal Fixed As PointF, ByVal Anchor As Integer, ByVal CheckEvents As Boolean)
        MyBase.New(Größe, Fixed, Anchor, CheckEvents)
    End Sub
    Public Overrides Function Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).Keydown(sender, e)
            Next
        Else

        End If
    End Function

    Public Overrides Function KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).KeyUp(sender, e)
            Next
        Else

        End If
    End Function

    Public Overloads Overrides Function MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If Not HasFocus Then
            CheckScrollBar(e.X, e.Y)
            If CheckEvents Then
                For I = 0 To PanelObjekte.Count - 1
                    PanelObjekte(I).MouseDown(sender, e)
                Next
            Else

            End If
        End If
    End Function

    Public Overloads Overrides Function MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).MouseEnter(sender, e)
            Next
        Else

        End If
    End Function

    Public Overloads Overrides Function MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As String
        MyBase.MouseUpScrollBar()
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                Dim F As String = PanelObjekte(I).MouseUp(sender, e)
                If F <> "" Then
                    Return F
                End If
            Next
        Else
        End If
        Return ""
    End Function

    Public Overloads Overrides Sub MouseMove(ByVal X As Integer, ByVal Y As Integer)
        MyBase.MouseMoveScrollBar(X, Y)
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).MouseEnter(X, Y)
            Next
        Else

        End If
    End Sub
End Class
Public Class ObjektPanelEinheitenMenü
    Inherits ObjektPanel
    Dim EinheitenMenü As ObjektEinheitenCheckbox
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Größe"></param>
    ''' <param name="Fixed"></param>
    ''' <param name="Anchor">0=Keiner,1=Alles,2=Rechts Links Fixed,3=Alle Ecken bewegen sich,4=Links,Rechts,Unten</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal Größe As Rectangle, ByVal Fixed As PointF, ByVal Anchor As Integer, ByVal CheckEvents As Boolean)
        MyBase.New(Größe, Fixed, Anchor, CheckEvents)
    End Sub
    Public Sub SetEinheitenMenü(ByVal E As ObjektEinheitenCheckbox)
        Me.EinheitenMenü = E
        CheckScrollBarActivationEinheitenBoxen()
    End Sub
    Private Function CheckScrollBarActivationEinheitenBoxen() As Boolean
        Dim Höhe As Integer = (Math.DivRem(Objektverwalter.AlleEinheiten.Length - 1, EinheitenMenü.AnzahlderEinheiteninderBreite, 0) + 1) * EinheitenMenü.Höhe
        Dim Breite As Integer = EinheitenMenü.AnzahlderEinheiteninderBreite * EinheitenMenü.Breite
        If Not InVisibleArea(New Rectangle(EinheitenMenü.StartPosition.X - 10, EinheitenMenü.StartPosition.Y - 10, Breite, Höhe), True) Then
            ActivateScrollBars()
            If RealDimension.Width < Breite + 10 Then
                RealDimension.Width = Breite + 10
            End If
            If RealDimension.Height < Höhe + 10 Then
                RealDimension.Height = Höhe + 10
            End If
            Return True
        End If
        Return False
    End Function
    Public Overrides Function Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).Keydown(sender, e)
            Next
        Else

        End If
    End Function
    Protected Overrides Sub CheckScrollBarActivation()
        Dim Activation As Boolean = CheckScrollBarActivationEinheitenBoxen()
        If Not Activation Then
            MyBase.CheckScrollBarActivation()
        End If
    End Sub
    Public Overrides Function KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).KeyUp(sender, e)
            Next
        Else

        End If
    End Function

    Public Overloads Overrides Function MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        CheckScrollBar(e.X, e.Y)
        If CheckEvents Then
            If EinheitenMenü.CheckKlick(e.X, e.Y) Then
                Return True
            Else
                For I = 0 To PanelObjekte.Count - 1
                    PanelObjekte(I).MouseDown(sender, e)
                Next
            End If
        Else

        End If
    End Function

    Public Overloads Overrides Function MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).MouseEnter(sender, e)
            Next
        Else

        End If
    End Function

    Public Overloads Overrides Sub MouseMove(ByVal X As Integer, ByVal Y As Integer)
        MyBase.MouseMoveScrollBar(X, Y)
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                PanelObjekte(I).MouseEnter(X, Y)
            Next
        Else

        End If
    End Sub

    Public Overloads Overrides Function MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As String
        MyBase.MouseUpScrollBar()
        If CheckEvents Then
            For I = 0 To PanelObjekte.Count - 1
                Dim F As String = PanelObjekte(I).MouseUp(sender, e)
                If F <> "" Then
                    Return F
                End If
            Next
        Else
        End If
        Return ""
    End Function

    Public Overrides Sub RemoveHandles()
        EinheitenMenü = Nothing
        MyBase.RemoveHandles()
    End Sub
End Class
